package com.fqdf.framework.android.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import com.fqdf.framework.android.view.listener.LoadMoreListener;


public class LoadMoreListView extends ListView implements OnScrollListener {

    private boolean isCanLoadMore = false;

    // 加载更多的事件回调
    private LoadMoreListener mAutoLoadListener;


    public LoadMoreListView(Context context) {
        super(context);
        initInternalView(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initInternalView(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initInternalView(context);
    }

    private void initInternalView(Context context) {
//        View rootView = LayoutInflater.from(context).inflate(R.layout.load_more_footer, null);
        setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        switch (i) {
            // 当不滚动时
            case OnScrollListener.SCROLL_STATE_IDLE:
                // 判断滚动到底部
                if (absListView.getCount() > 0) {
                    if (absListView.getLastVisiblePosition() == (absListView.getCount() - 1)) {
                        if (isCanLoadMore) {
                            mAutoLoadListener.autoLoadMore();
                        }
                    }
                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

    }

    /**
     * 设置是否可以加载更多
     *
     * @param bool
     */
    public void setCanLoadMore(boolean bool) {
        isCanLoadMore = bool;
    }

    public void setLoadMoreListener(LoadMoreListener loadMoreListener) {
        this.mAutoLoadListener = loadMoreListener;
    }

    /**
     * 活动到顶部，顺利滑动，附带滑动动画
     */
    public void smoothScrollToTop() {
        smoothScrollToPosition(0);
    }

    /**
     * 活动到顶部，顺利滑动
     */
    public void scrollToTop() {
        setSelection(0);
    }

}
